package com.songlanyun.modules.rights.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.math.BigDecimal;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.common.enums.ShopGainsConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.modules.exception.OrderException;
import com.songlanyun.modules.order.entity.ChildOrder;
import com.songlanyun.modules.order.entity.Order;
import com.songlanyun.modules.order.service.ChildOrderService;
import com.songlanyun.modules.order.service.OrderService;
import com.songlanyun.modules.rabbitmq.util.SpringContextUtil;
import lombok.Data;

import javax.validation.constraints.NotNull;

/**
 * 销售系统的维权单的子单
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2020-11-23 13:45:42
 */
@Data
@TableName("yxt_sale_rights_child_order")
public class RightsChildOrder extends RootEntity<Long,RightsChildOrder> implements Serializable {
	private static final long serialVersionUID = 1L;


	{
		inStockNum = 0;
		thirdSkuId = 0L;
	}
	/**
	 *
	 */
	@TableId
	private Long id;
	/**
	 * 维权订单id
	 */
	@NotNull(message = "维权订单id不能为空")
	private Long rightsId;
	/**
	 * 所属商家id
	 */
	@NotNull(message = "所属商家id不能为空")
	private Long shopId;
	/**
	 *
	 */
	@NotNull(message = "所属订单id不能为空")
	private Long orderId;
	@TableField(exist = false)
	private Order order;
	@JsonIgnore
	public Order getOrder()
	{
		if(order == null)
		{
			OrderService orderService = (OrderService) SpringContextUtil.getBean("orderService");
			order = orderService.getById(getOrderId());
		}
		return order;
	}
	/**
	 * 所属子订单id
	 */
	@NotNull(message = "所属子订单id不能为空")
	private Long childOrderId;
	@TableField(exist = false)
	private ChildOrder childOrder;
	@JsonIgnore
	public ChildOrder getChildOrder()
	{
		if(childOrder == null)
		{
			ChildOrderService childOrderService = (ChildOrderService) SpringContextUtil.getBean("childOrderService");
			childOrder = childOrderService.getById(getChildOrderId());
		}
		return childOrder;
	}
	/**
	 * 退款金额
	 */
	private BigDecimal refundMoney;
	/**
	 * 退款商品数量
	 */
	private Integer refundNum;

	/**
	 * 退还积分
	 */
	private BigDecimal refundPoint;

	/**
	 * 入库存数
	 */
	private Integer inStockNum;




	/**
	 * 入库
	 * @param num
	 */
	public void  inStock(Integer num)
	{
		if(this.getInStockNum()+num > this.getRefundNum())
		{
			throw new RRException(OrderException.RIGHTS_ORDER_IN_STOCK_EX);
		}
		this.inStockNum +=num;
	}


	/**
	 * 商品Id
	 */
	@NotNull(message = "商品Id不能为空")
	private Long goodsId;
	/**
	 * 商品标题
	 */
	private String goodsTitle;
	/**
	 * 商品图片
	 */
	private String goodsImg;
	/**
	 * 原规格销售价
	 */
	private BigDecimal salePrice;
	/**
	 * 商品sku编码
	 */
	private String skuBarCode;
	/**
	 * 商品sku字面量
	 */
	private String skuSetName;

	/**
	 * 商品sku hash值
	 */
	private String skuHashCode;

	/**
	 * 第三方商品skuId
	 */
	private Long thirdSkuId;

	/**
	 * 所属单品id
	 */
	@NotNull(message = "商品Id不能为空")
	private Long singleGoodsId;

	@NotNull(message = "商品类型不能为空")
	private ShopGainsConstant.GoodsType goodsType;

	@Override
	public Long getIdentity() {
		return getId();
	}
}
